/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rockville.ufone.rssdata;

import java.net.URL;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.xml.DOMConfigurator;

import com.rockville.ufone.DBProc;
import com.rockville.ufone.SystemProperty;
import com.rockville.util.StringUtil;
import com.rockville.xms.SendXMSManager;

/**
 * 
 * @author M Bilal
 */
public class ReSendWapPush implements Runnable {

    private static final Logger log = Logger.getLogger(ReSendWapPush.class);
    private static final String strWapPush = "http://wap.ufone.com/ufone/download.jsp?entry=";
    private static final String strUGreet = "http://wap.ufone.com/ufone/ugreetings/ucdh?sid=";

    public ReSendWapPush() {
    }

    public void run() {
        log.info("START Process ReSendWapPush to Billed Users");
        DBProc db = DBProc.getInstance();
        try {
            // seqid,content_id,category_type,msisdn,shortcode
            Vector<String[]> v = db.getReSendWapPushUsers();
            for (int i = 0; i < v.size(); i++) {
                String[] s = v.get(i);
                String wapPush = "";
                String strGsm = s[3];
                String strShortCode = s[4];
                String strResp = "";
                String strMsg = "";
                String strContentType = s[2];
                String strSeqId=s[0];

                try {
                    if ("UGreetings".equals(strContentType)) {
                        String strCid = s[1].substring(5).trim();
                        wapPush = strUGreet + strCid;
                        String[] arrCardInfo = db.getGreetingCardInfo(strCid);
                        // ContentId,CatID,CatName,Sender,Rcv_MSISDN						
                        strMsg = arrCardInfo[3] + "(" + strGsm + ") has sent you UGreetings Card powered by Ufone, Please Click the link to download";
                        strGsm = arrCardInfo[4];
                        strResp = SendXMSManager.sendWAPPush(strGsm,
                                SystemProperty.getProperty("smpp_provider"),
                                strShortCode, strMsg, wapPush);
                        log.debug("WAPPUSH text=" + strMsg + ", link=" + wapPush
                                + " ;Send to MSISDN=" + strGsm
                                + " ;WAP PUSH Responce=" + strResp);
                    } else {
                        strContentType = strContentType.replace("ANIMATED",
                                "Animations");
                        strContentType = strContentType.replace("TRUETONES",
                                "Ringtones");
                        strContentType = strContentType.replace("POLYTONES",
                                "Ringtones");
                        strContentType = strContentType.substring(0,
                                strContentType.length() - 1).toLowerCase();
                        strContentType = com.rockville.util.StringUtil.capitalizeSentense(strContentType);
                        strSeqId = StringUtil.encryptString(String.valueOf(strSeqId), SystemProperty.getProperty("msisdn_encrypt_key"));
                        wapPush = strWapPush + strSeqId;
                        strMsg = "Dear User, You have been charged for "
                                + strContentType
                                + " content download from Ufone WAP, Please click the link to download";
                        strResp = SendXMSManager.sendWAPPush(strGsm,
                                SystemProperty.getProperty("smpp_provider"),
                                strShortCode, strMsg, wapPush);
                        log.debug("WAPPUSH text=" + strMsg + ", link=" + wapPush
                                + " ;Send to MSISDN=" + strGsm
                                + " ;WAP PUSH Responce=" + strResp);
                    }
                } catch (Exception e) {
                    log.error("Error for send WAP PUSH for Content="
                            + strContentType + ";strShortCode=" + strShortCode
                            + "; strGsm=" + strGsm, e);
                }
            }

        } catch (Exception e) {
            log.error("ReSendWapPush.process()", e);
        } finally {
        }
        log.info("END Process ReSendWapPush to Billed Users");
    }

    public static void main(String[] args) {
        try {
            URL url = Loader.getResource("log4j-config.xml");
            DOMConfigurator.configure(url);
            ReSendWapPush rswp = new ReSendWapPush();
            rswp.run();
            
        } catch (Exception e) {
            log.error("", e);
        }
    }
}
